<?php

include(dirname(__FILE__).'/../../bootstrap/functional.php');

$browser = new TravideTestFunctional(new sfBrowser());

$browser->setUp();

/**
 * Retrieve the last added collection
 * in this case from the test data so we can refer to it in links
 */
$latestCollectionItem = $browser->getLastestCollection();
$lastestCollectionType = $browser->getLatestCollectionType();

$browser->info('System Requrements Testing - Refer Issues Tracker for Issue IDs');
$browser->info('E## = Enhancement ## from the Issue Tracker');

$browser->info('E38 - Users must be authenticated');
$browser->info('E38 - Will login as Admin to ensure tests can be run');
$browser->info('E38 - Security should be tested with a different set of tests')->

        get('guard/login')->
        with('request')->begin()->
        isParameter('module','sfGuardAuth')->
        isParameter('action','signin')->
        end()->

        with('response')->begin()->
        isStatusCode(401)->
        // isRedirected()->followRedirect()->
        end()->

        click('form input[value="Signin"]',array('signin' =>
        array('username' => 'admin', 'password' => 'admin')
        ))->

        with ('request')->begin()->
        isParameter('module','sfGuardAuth')->
        isParameter('action','signin')->
        end()->

        with('form')->begin()->
        hasErrors(false)->
        end()->


        with('user')->begin()->
        isAuthenticated()->
        end();
/*
        with('response')->begin()->
        isStatusCode(200)->
        debug()->
        isRedirected()->followRedirect()->
        end()->

        with ('request')->begin()->
        isParameter('module','search')->
        isParameter('action','query')->
        end();
*/
// isRedirected()->followRedirect();



/**
 *
 * E29 - Users should be able to view Collections
 *
 */
$browser->info('E29 - Show Collections');
$browser->info('E29.1 Existing Collection')->
        get('collection/'.$latestCollectionItem->getUin())->
        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','show')->
        end()->

        with('response')->begin()->
        isStatusCode(200)->
        end();

$browser->info('E29.2 Non-existant Collection')->
        get('collection/'.($latestCollectionItem->getUin()-1))->
        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','show')->
        end()->

        with('response')->begin()->
        isStatusCode(404)->
        end();

/**
 *
 * E17 - Users should be able to add collections
 * E24 Allow users to specify the type of collection
 *
 */

$browser->info('E17 - Adding Collections');
$browser->info('E17.1 - Successfully Add Collection')->

        get('collection/new')->
        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','new')->
        end()->

        with('response')->begin()->
        isStatusCode(200)->
        checkElement('#save')->
        end()->

        /**
         * Also tests E24 allowing users to specify a type of collection
         */
        info('E24.1 - Allow users to specify the type of collection')->
        click('#save',array('collection_item' =>
        array('collectionName' => 'E17.1 Test Collection',
                'collectionTypeUin' => $lastestCollectionType->getUin()
        )))->

        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','create')->
        end()->

        with('form')->begin()->
        hasErrors(false)->
        end()->

        with('doctrine')->begin()->
        check('CollectionItem', array(
        'collectionName' => 'E17.1 Test Collection',
        'collectionTypeUin' =>  $lastestCollectionType->getUin()
        ))->
        end()->

        with ('response')->
        isRedirected()->followRedirect()->
        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','edit')->
        end();
/**
 *
 * E18 - Users should be able to Edit collections
 *
 */
$latestCollectionItem = $browser->getLastestCollection();

$browser->info('E18 - Editing Collections');
$browser->info('E18.1 - Successfully Edit Collection')->
        get('collection/'.$latestCollectionItem->getUin().'/edit')->
        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','edit')->
        end()->

        with('response')->begin()->
        isStatusCode(200)->
        checkElement('#collection_item_collectionName')->
        checkElement('#collection_item_uin')->
        checkElement('#collection_item_collectionTypeUin')->
        checkElement('#collection_item__csrf_token')->
        checkElement('#save')->
        end()->

        click('#save',array('collection_item' =>
        array('collectionName' => 'E18.1 Test Collection',
                'collectionTypeUin' => $lastestCollectionType->getUin()
        )))->

        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','update')->
        end()->

        with('form')->begin()->
        hasErrors(false)->
        end()->

        with('doctrine')->begin()->
        check('CollectionItem', array(
        'collectionName' => 'E18.1 Test Collection',
        'collectionTypeUin' =>  $lastestCollectionType->getUin()
        ))->
        end()->

        with ('response')->
        isRedirected()->followRedirect()->
        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','edit')->
        end();


/**
 *
 * E19 - Delete Collections
 *
 */
$latestCollectionItem = $browser->getLastestCollection();

$browser->info('E19 - Deleting Collections');
$browser->info('E34 - SoftDelete or Archive Collections');
$browser->get('collection/'.$latestCollectionItem->getUin().'/edit')->
        click('#delete')->
        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','delete')->
        end()->

        with('doctrine')->begin()->
        check('CollectionItem', array(
        'collectionName' => $latestCollectionItem->getCollectionName(),
        'collectionTypeUin' =>  $latestCollectionItem->getCollectionTypeUin(),
        'deleted_at' => null
        ),false)->
        end()->

        with ('response')->
        isRedirected()->followRedirect()->
        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','show')->
        end()->

        with('response')->begin()->
        isStatusCode(200)->
        checkElement('#archivedMessage')->
        end();

$browser->info('E34 - Restore collection')->
        click('#restore')->

        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','restore')->
        end()->


        with ('response')->
        isRedirected()->followRedirect()->
        with('request')->begin()->
        isParameter('module','collection')->
        isParameter('action','show')->
        end()->


        with('response')->begin()->
        isStatusCode(200)->
        checkElement('#archivedMessage',false)->
        end()->

        with('doctrine')->begin()->
        check('CollectionItem', array(
        'collectionName' => $latestCollectionItem->getCollectionName(),
        'collectionTypeUin' =>  $latestCollectionItem->getCollectionTypeUin(),
        'deleted_at' => null
        ))->
        end();


/**
 *
 * E20 - Search all collections in a repository -
 * simple search
 *
 */

/**
 *
 * E21 - add properties to a collection
 *
 */

/**
 *
 * E22 - add relations to a collection
 *
 */

/**
 *
 * E23 - add resources to a collection
 *
 */

/**
 *
 * E24 - The type of collection
 *
 */

/**
 *
 * E25 - Update and delete properites when editing a collection
 *
 */

/**
 *
 * E26 - Users should be able to execute a  basic search from any page
 *
 */
$browser->info('E26 - Basic Search from any page');
$browser->info('E26.1 - Test New Collection Page')->
        get('collection/new')->
        click('Search',array('queryString' => 'test'))->

        with('request')->begin()->
        isParameter('module','search')->
        isParameter('action','query')->
        end()->

        with('response')->begin()->
        isStatusCode(200)->
        end();

/**
 *
 * E27 - Advanced Search
 *
 */

/**
 *
 * E28 - Persistent URI
 *
 */